﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using eMisc;

namespace eLib.ImportWizard
{
    public partial class frmXlsLoaderScalars : Form
    {
        public frmXlsLoaderScalars()
        {
            InitializeComponent();
        }

        public static bool EditLoaderScalars(BaseData data, XlsLoader ldr)
        {
            using (frmXlsLoaderScalars f = new frmXlsLoaderScalars())
            {
                f.xlsSelect.FileName = ldr.FileName;
                f.xlsSelect.SheetName = ldr.SheetName;
                f.xlsSelect.ResetChanges();
                f.Text = "Настройки импорта. "+data.Name;
                f._fillTo(data, ldr);

                bool res = f.ShowDialog() == DialogResult.OK && (f.changed || f.xlsSelect.Changed);
                if (res)
                    f._fillFrom(data, ldr);
                return res;
            }
        }

        private void _fillTo(BaseData d, XlsLoader _ldr)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(
                new DataColumn[] { new DataColumn("cParam", typeof(string)),
                                   new DataColumn("cCell", typeof(string)) });
            List<string> paramNames = new List<string>();
            d.GetAvailableParamNames(paramNames, true);
            foreach (string s in paramNames)
            {
                DataRow r = dt.NewRow();
                r["cParam"] = s;

                XlsLoaderItemType itype;
                string cell, dummy_paramCol;

                _ldr.getInfoLoadData(d.Name, s, out itype, out cell, out dummy_paramCol);
                if (itype == XlsLoaderItemType.xiCell)
                    r["cCell"] = cell;

                dt.Rows.Add(r);
            }
            dgv.DataSource = dt;
            changed = false;
        }

        private void _fillFrom(BaseData d, XlsLoader _ldr)
        {
            _ldr.Reset();
            DataTable dt = (DataTable)dgv.DataSource;
            foreach (DataRow r in dt.Rows)
                if (r["cCell"] != DBNull.Value && r["cCell"].ToString().Trim() != "")
                    _ldr.AddLoadCellInfo(d.Name, r["cParam"].ToString(), r["cCell"].ToString());
            _ldr.DataNameTo = d.Name;
            _ldr.FileName = xlsSelect.FileName;
            _ldr.SheetName = xlsSelect.SheetName;
        }

        bool changed = false;

        private void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgv.Columns[e.ColumnIndex].Name == "cCell")
            {
                string err;
                if (!string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    string s = e.FormattedValue.ToString();
                    if (WorkSheetUtils.CheckCell(ref s, out err))
                    {
                        MessageBox.Show(err, Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                        return;
                    }
                }
                changed = true;
            }
        }

        private void dgv_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            string s="";
            if (!string.IsNullOrEmpty(e.Value.ToString()))
                foreach(char c in e.Value.ToString().Trim())
                    if (char.IsLetterOrDigit(c)) s+=c;
            e.Value = s;
        }
    }
}